//package org.example.ymcc.config;
//
//import com.alibaba.fastjson.JSON;
//import lombok.extern.slf4j.Slf4j;
//import org.example.ymcc.result.JsonResult;
//import org.example.ymcc.util.jwt.JwtUtils;
//import org.example.ymcc.util.jwt.Payload;
//import org.example.ymcc.util.jwt.PayloadData;
//import org.example.ymcc.util.jwt.RsaUtils;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.cloud.gateway.filter.GatewayFilterChain;
//import org.springframework.cloud.gateway.filter.GlobalFilter;
//import org.springframework.core.Ordered;
//import org.springframework.core.io.buffer.DataBuffer;
//import org.springframework.http.server.RequestPath;
//import org.springframework.http.server.reactive.ServerHttpRequest;
//import org.springframework.http.server.reactive.ServerHttpResponse;
//import org.springframework.stereotype.Component;
//import org.springframework.util.FileCopyUtils;
//import org.springframework.web.server.ServerWebExchange;
//import reactor.core.publisher.Mono;
//
//import java.io.IOException;
//import java.security.PublicKey;
//import java.util.List;
//
///**
// * 获取请求头，解析token，判断非空，刷新过期时间；如果是空返回一个“未登录”
// */
//@Slf4j
//@Component
//public class AuthenticationFilter implements GlobalFilter, Ordered {
//    @Autowired
//    private ExcludeProperties excludeProperties;   //要放行的接口地址集合
//
//    @Override
//    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
//        //1.先获取请求对象、响应对象  gateway是基于webflux封装的，它的请求、响应对象与HttpServletRequest不一样了
//        ServerHttpRequest request = exchange.getRequest();
//        ServerHttpResponse response = exchange.getResponse();
//
//        //2.放行我们在yml中配置的接口地址
//        RequestPath path = request.getPath();
//        log.info("本次请求地址：{}", path.value());
//        if (excludeProperties.isExcluded(path.value())){
//            return chain.filter(exchange);
//        }
//
//        //3.获取请求头
//        List<String> headers = request.getHeaders().get("token");
//
//        try {
//            //4.判断
//            if(headers != null && headers.size() > 0){
//                String token = headers.get(0);
//                //5.利用JWT工具类解析token   私钥加密 公钥解密
//                PublicKey publicKey = RsaUtils
//                        .getPublicKey(FileCopyUtils
//                                .copyToByteArray(this.getClass().getClassLoader().getResourceAsStream("hrm_auth_rsa.pub")));
//                Payload<PayloadData> payload = JwtUtils.getInfoFromToken(token, publicKey, PayloadData.class);
//                if(payload != null){
//                    PayloadData payloadData = payload.getUserInfo();
//                    if(payloadData != null){
//                        Object login = payloadData.getLogin();
//                        if(login != null){
//                            //表示已登录  直接放行
//                            return chain.filter(exchange);
//                        }
//                    }
//                }
//            }
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//
//        //6.回“noLogin”
//        response.getHeaders().set("content-type","application/json;charset=UTF-8");
//        JsonResult jsonResult = JsonResult.error("noLogin");
//        byte[] bytes = JSON.toJSONBytes(jsonResult);//阿里巴巴的json
//        DataBuffer wrapper = response.bufferFactory().wrap(bytes);
//        return response.writeWith(Mono.just(wrapper));
//    }
//
//    @Override
//    public int getOrder() {
//        return 0;
//    }
//}